package com.littlebee.storychild.ui;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.littlebee.storychild.R;
import com.littlebee.storychild.common.utils.GsonUtils;
import com.littlebee.storychild.common.utils.LogUtil;
import com.littlebee.storychild.common.utils.NetWorkUtils;
import com.littlebee.storychild.common.utils.Utils;
import com.littlebee.storychild.ui.entry.MyStoryEntry;
import com.littlebee.storychild.ui.entry.StoryEntry;
import com.littlebee.storychild.ui.entry.User;
import com.zss.library.activity.BaseActivity;
import com.zss.library.toolbar.CommonToolbar;
import com.zss.library.utils.LogUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by liaoyong on 14/12/2017.
 * 图片播放故事详情界面
 */

public class StoryInfoImagePlayActivity extends BaseActivity {
    private ImageView mPlayImageView;
    private CommonToolbar toolbar;

    private MyStoryEntry storyEntry = new MyStoryEntry();
    @Override
    public int getLayoutResId() {
        return R.layout.activity_image_play_story;
    }

    @Override
    public void initView() {
        super.initView();
        String storyId = getIntent().getStringExtra("storyId");
        storyEntry.setStoryid(storyId);
        mPlayImageView = (ImageView) findViewById(R.id.iv_image);
    }

    @Override
    public void setTopBar() {
        super.setTopBar();
        setStatusBarColor(R.color.module_video_bg);
        toolbar = getToolbar();
        toolbar.setBgRes(R.color.trans);
        toolbar.setTitle("故事详情");
    }

    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);//must store the new intent unless getIntent() will return the old one
        int page = getIntent().getIntExtra("page", 0);
        String storyId = getIntent().getStringExtra("storyId");
        if (storyEntry == null || storyEntry.getStorypages() == null || !storyId.equals(storyEntry.getStoryid())) {
            if (storyEntry != null) {
                storyEntry.setStoryid(storyId);
            }
            getStoryInfo();
        } else {
            refreshImageView(page);
        }
    }

    @Override
    public void setIntent(Intent newIntent) {
        super.setIntent(newIntent);
     }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        getStoryInfo();
    }

    /*
    * 拉取故事
    *
    * */
    public void getStoryInfo() {
        //phoneId=18611697983&storyid=1
        Map<String, String> hashMap = new HashMap<>();
        User user = Utils.getUserEntry();
        hashMap.put("phoneId", user.getMobile());
        hashMap.put("storyid", storyEntry.getStoryid());
        NetWorkUtils.post(getActivity(), "getstoryinfo", hashMap, true, new NetWorkUtils.IListener() {

            @Override
            public void onSuccess(String result, JSONObject resObj) {
                String res = resObj.optString("response")/**.optString("storypages")*/;
                storyEntry = GsonUtils.getObjFromJSON(res, MyStoryEntry.class);
                toolbar.setTitle(storyEntry.getStoryname());
                refreshImageView(0);
            }

            @Override
            public void onError(String result, String code, String msg) {
                LogUtils.i("XHX", "XHXERROR:" + result);


            }
        });
    }

    private void refreshImageView(int index) {
        LogUtil.d("liaoyong: image url :" + storyEntry.getStorypages().get(index).getPageurl());
        Glide.with(this)
                .load(storyEntry.getStorypages().get(index).getPageurl())
                .placeholder(R.color.module_video_bg)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(mPlayImageView);
    }
}
